package cn.vconinfo.app.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLEncoder;

@Slf4j
public class FileUtils {

    public static boolean downloadFile(String downloadFile, HttpServletResponse response) {
        boolean isHandler = false;
        // String downloadFile = getDownloadFile();
        if (StringUtils.isNotBlank(downloadFile)) {
            // 实现文件下载
            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                // 配置文件下载
                // response.setHeader("content-type",
                // "application/octet-stream");
                response.setContentType("application/octet-stream");
                response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
                File file = new File(downloadFile);
                if (file.exists()) {
                    // 下载文件能正常显示中文
                    response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                } else {
                    // 下载文件能正常显示中文
                    // 获取jar包里面文件
                    response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(downloadFile, "UTF-8"));
                    InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(downloadFile);
                    if (inputStream == null) {
                        ClassPathResource classPathResource = new ClassPathResource(downloadFile);
                        inputStream = classPathResource.getInputStream();
                    }
                    bis = new BufferedInputStream(inputStream);
                }
                OutputStream os = response.getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                os.flush();

                // BlobUtil blob = new BlobUtil(fis);
                // BufferedOutputStream output = new BufferedOutputStream(os);
                // output.write(blob.getBytes(0,
                // Integer.parseInt(String.valueOf(blob.length()))));

                isHandler = true;
            } catch (Exception e) {
                isHandler = false;
                log.error("文件【{}】下载失败:", downloadFile, e);
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return isHandler;
    }

    /**
     *  获取文件的类路径下的绝对路径
     *
     * @param fileName
     * @return
     */
    public static String getAbsolutePath(final String fileName){
        String filePath = fileName;
        File file = new File(fileName);
        if(!file.exists()) {
            URL requestPath = ClassLoader.getSystemResource(fileName);
            if(requestPath == null) {
                filePath = fileName;
            }else {
                filePath = requestPath.getFile();
            }
        }

        return filePath;
    }

}
